// -------------------------------------------
//					Word
// -------------------------------------------

#include "Word.h"
#include <cctype>
#include <iostream>

using namespace std;

Word::Word(const string& word): _word(word)
{
    size_t size = word.size();
    // throws an exception (in the form of WordContainsNoLetters object)
	// indicating that the word being constructed contains no letters
	if (_word.empty()) throw WordContainsNoLetters();
    ///check if there is a space
    size_t foundSpace = word.find_first_of(" ");
    if ( foundSpace != string::npos )
    {


        throw WordContainsSpace();
    }

	///converts from uppercase to lowercase and checks for punctuations
    size_t noPunct = 0;
    size_t noLetters = 0;
    size_t removePunct = 0;

	for( int i = 0; i < size; i++ )
	{
	    char letter = word[i];

        if ( isupper( letter ) )            //convert uppercase to lowercase
        {
            _word[i] = tolower( letter );
        }

        if ( isalpha( letter ) && !isupper( letter )) //sifts out the letters
        {
            _word[ noPunct ] = letter;
            ++noPunct;
        }

        if ( !isalpha(letter))              //counts each punctuation
        {
            ++noLetters;
        }

        if( noLetters == size )             //check if string is entirely punctuation
        {
            throw WordContainsNoLetters();
        }

    }

    ///remove any remaining punctuation
    removePunct = _word.find_first_of("~`!@#$%^&*()-_+={}[]\\|:;'<>?,./\"");
    if ( removePunct != string::npos )
    {
        _word.erase( removePunct );
    }

	// Note, we will cover exceptions in more detail later on in the course.
}

// overloads the equivalence operator which allows to Words to be compared using ==
bool Word::operator==(const Word& rhs) const
{
	if (_word == rhs._word)
		return true;
	else
		return false;
}

bool Word::isQueryable() const
{
    if (_word.size() >= 3)
    {
        return true;
    }

    if (_word.size() < 3)
    {
        return false;
    }
}

/*
void Word::display()
{
    cout << _word << endl;
    return;
}
*/
